const http = require("http");
const fs = require("fs");

const server = http.createServer((req,res) => {
    console.log("服务器启动了");
    
    let url = req.url.split(".");
    console.log(url);

    if (url[1] == "html") {
        fs.readFile(`src/page${url[0]}.html`, "utf-8", (err,data) => {
            res.writeHead(200, {"content-type":"text/html;charset=utf-8"});
            res.write(data);
            res.end();
        })
    };
    if (url[1] == "css") {
        fs.readFile(`src${url[0]}.css`, "utf-8", (err,data) => {
            res.writeHead(200, {"content-type":"text/css;charset=utf-8"});
            res.write(data);
            res.end();
        })
    };
    if (url[1] == "js") {
        fs.readFile(`src${url[0]}.js`, "utf-8", (err,data) => {
            res.writeHead(200, {"content-type":"text/javascript;charset=utf-8"});
            res.write(data);
            res.end();
        })
    };
    if (url[1] == "JPG" || url[1] == "jpeg" || url[1] == "png") {
        fs.readFile(`src${req.url}`, (err,data) => {
            res.writeHead(200, {"content-type":`image/${url[1]};`});
            res.write(data);
            res.end();
        })
    }; 
});
server.listen(9000);